package com.david.viewtest.view

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup

class SwipeCellsView(
    context: Context,
    attrs: AttributeSet,
) : ViewGroup(context, attrs) {
    private var halfWidth = 0


    /**
     * 动画对象
     */
    private val valueAnimator: ValueAnimator = ValueAnimator.ofFloat(0f, 1f)

    /**
     * 动画执行进度，0-1
     */
    private var animProgress = 0f

    init {
        valueAnimator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
            override fun onAnimationUpdate(animation: ValueAnimator) {
                animProgress = animation.animatedValue as Float

                lastOffsetLeft = (downX + distanceX * animProgress).toInt()
                scrollTo(lastOffsetLeft, 0)
                //postInvalidate()
            }
        })

        valueAnimator.duration = 200

        //valueAnimator.addListener(object : Animator.AnimatorListener {
        //    override fun onAnimationStart(animation: Animator) {}
        //
        //    override fun onAnimationEnd(animation: Animator) {
        //    }
        //
        //    override fun onAnimationCancel(animation: Animator) {}
        //
        //    override fun onAnimationRepeat(animation: Animator) {}
        //})
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val selfWidth = MeasureSpec.getSize(widthMeasureSpec)
        val selfHeight = MeasureSpec.getSize(heightMeasureSpec)

        for (i in 0 until childCount) {
            val params = getChildAt(i).layoutParams
            val widthSpec = MeasureSpec.makeMeasureSpec(selfWidth / 2,
                MeasureSpec.EXACTLY)
            //val widthSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft, params.width)
            val heightSpec = getChildMeasureSpec(heightMeasureSpec, paddingLeft, params.height)
            val child = getChildAt(i)
            child.measure(widthSpec, heightSpec) //子view测量
        }


        setMeasuredDimension(selfWidth, selfHeight)

        halfWidth = selfWidth / 2
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        val itemWidth = measuredWidth / 2
        for (i in 0 until childCount) {
            getChildAt(i).layout(itemWidth * i, 0, itemWidth * (i + 1), measuredHeight)
        }
    }

    private var lastOffsetLeft = 0
    private var currentIndex = 0


    private var downX = 0f
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                downX = event.x
            }

            MotionEvent.ACTION_MOVE -> {
                scrollTo(lastOffsetLeft - (event.x - downX).toInt(), 0)
            }

            MotionEvent.ACTION_UP -> {
                //val tempIndex = event.x.toInt() / halfWidth
                if ((event.x - downX) > 30) {
                    if (currentIndex == 1) {
                        currentIndex = 0
                        //startScroll(lastOffsetLeft - (event.x - downX), currentIndex * halfWidth.toFloat())
                    } else if (currentIndex > 1) {
                        currentIndex -= 2
                        //startScroll(lastOffsetLeft - (event.x - downX), currentIndex * halfWidth.toFloat())
                    }
                } else if ((downX - event.x) > 30) {
                    if (currentIndex == childCount - 3) {
                        currentIndex += 1
                        //startScroll(lastOffsetLeft - (event.x - downX), currentIndex * halfWidth.toFloat())
                    } else if (currentIndex < childCount - 2) {
                        currentIndex += 2
                        //startScroll(lastOffsetLeft - (event.x - downX), currentIndex * halfWidth.toFloat())
                    }
                }
                //else {
                //}

                startScroll(lastOffsetLeft - (event.x - downX), currentIndex * halfWidth.toFloat())

                //lastOffsetLeft -= (event.x - downX).toInt()

            }

            MotionEvent.ACTION_CANCEL -> {}
        }
        return true
    }

    private var distanceX = 0f
    private fun startScroll(startX: Float, endX: Float) {
        downX = startX
        distanceX = endX - startX
        valueAnimator.start()
    }
}